<?php
namespace jy_farmer;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	//订单创建时，执行此方法，执行相关操作
	public $sendtypes=array(
		'1'=>'快递配送',
		'2'=>'自提/核销',
		'3'=>'虚拟发货',
	);
	//发货
	public function order_send($order_id,$params=array()){
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'orderid'=>$order_id,
			'express_id'=>intval($params['express']),
			'expresssn'=>$params['expresssn'],
			'opensend_type'=>$params['opensend_type'],
			'createtime'=>TIMESTAMP,
		);
		$this->insert('order_express',$data);
		$this->update('order',array('status'=>3,'sendtime'=>time()),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	}
	public function add_baobei($order_id,$msg='',$oldstatus=2,$mid=0,$api_id=0,$api_pluginname=''){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'order_id'=>$order_id,
			'oldstatus'=>$oldstatus?:2,
			'api_id'=>$api_id,
			'api_pluginname'=>$api_pluginname,
			'mid'=>$mid,
			'msg'=>$msg?:'异常报备',
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('baobei_log',$data);
		$logid=pdo_insertid();
		$this->pdo_update('order',array('status'=>7),array('id'=>$order_id));
		return $logid;
	}
	public function pay($order=array()){
		//优惠券已使用
		if($order['couponid']){
			model('jy_weishop/coupon')->usecoupon($order['couponid'],$order['ordersn']);
		}
		if($order['pay']>0 && $this->setting['jy_twojiup']){
			$jy_twojiup = $this->plugin_exists('jy_twojiup');
			if($jy_twojiup){
				$order=array(
					'mid'=>$order['mid'],
					'ordersn'=>$order['ordersn'],
					'price'=>$order['money'],
					'goods'=>array(),
				);
				model('jy_twojiup/commission')->create($order);
			}
		}
		return true;
	}
	//我要收益兑换现金
	public function order_shou1($logid,$logtype=0){
		if(!$logtype){
			$table='jy_farmer_gdopt';
		}elseif($logtype==1){
			$table='jy_farmer_plant';
		}elseif($logtype==2){
			$table='jy_farmer_adopt';
		}
		$log=pdo_get($table,array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$animal=pdo_get('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['guoshu_id']),array('down_send_type0','down_send_type1','image','title','classify_id','growtime','shouyue','shoukg','shoufee','oneprice'));
		$shoufee=$animal['oneprice']*$log['shoukg'];
		pdo_update($table,array('status'=>5,'shoutype'=>5,'shoufee'=>$shoufee),array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		$this->update('order',array('status'=>5,'sendtype'=>5),array('id'=>$log['order_id'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function getExpressList($express, $expresssn)
	{
		$express_set = $this->plugin_setting('express');
		$express_set['cache']=$express_set['cache']?:1;
		$express = ($express == 'jymwl' ? 'jiayunmeiwuliu' : $express);
		$express = ($express == 'TTKD' ? 'tiantian' : $express);
		$express = ($express == 'jjwl' ? 'jiajiwuliu' : $express);
		$express = ($express == 'zhongtiekuaiyun' ? 'ztky' : $express);
		if (!empty($express_set['isopen']) && !empty($express_set['apikey'])) {
			if (!empty($express_set['cache']) && (0 < $express_set['cache'])) {
				$cache_time = $express_set['cache'] * 60;
				$cache = $this->pdo_get('express_cache',array('express' => $express, 'expresssn' => $expresssn));
				if ((time() <= $cache['lasttime'] + $cache_time) && !empty($cache['datas'])) {
					return json_decode($cache['datas'],true);
				}
			}
			if ($express_set['isopen'] == 1) {
				$url = 'http://api.kuaidi100.com/api?id=' . $express_set['apikey'] . '&com=' . $express . '&nu=' . $expresssn;
				$params = array();
			}else {
				$url = 'http://poll.kuaidi100.com/poll/query.do';
				$params = array('customer' => $express_set['customer'], 'param' => ijson_encode(array('com' => $express, 'num' => $expresssn)));
				$params['sign'] = md5($params['param'] . $express_set['apikey'] . $params['customer']);
				$params['sign'] = strtoupper($params['sign']);
			}
			$response = cfc('http')->request($url, $params);
			$content = $response;
			$info = json_decode($content, true);
		}

		if (!isset($info) || empty($info['data']) || !is_array($info['data'])) {
			$url = 'https://www.kuaidi100.com/query?type=' . $express . '&postid=' . $expresssn . '&id=1&valicode=&temp=';
			$response = cfc('http')->request($url);
			$content = $response;
			$info = json_decode($content, true);
			$useapi = false;
		}else {
			$useapi = true;
		}
		$list = array();
		if (!empty($info['data']) && is_array($info['data'])) {
			foreach ($info['data'] as $index => $data) {
				$list[] = array('time' => trim($data['time']), 'step' => trim($data['context']));
			}
		}
		if ((0 < $express_set['cache']) && count($list)>1) {
			if (empty($cache)) {
				pdo_insert('jy_weishop_express_cache', array('expresssn' => $expresssn, 'express' => $express, 'lasttime' => time(), 'datas' => ijson_encode($list)));
			}else {
				pdo_update('jy_weishop_express_cache', array('lasttime' => time(), 'datas' => ijson_encode($list)), array('id' => $cache['id']));
			}
		}
		return $list;
	}
}